package com.feidian.helper.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.feidian.helper.R;
import com.feidian.helper.bean.Item;
import com.feidian.helper.bean.Table;
import com.feidian.helper.bean.User;
import com.feidian.helper.net.NetManager;
import com.feidian.helper.net.NetworkDetector;
import com.feidian.helper.ui.fragment.MessageFragment;
import com.feidian.helper.ui.view.MyDialog;
import com.feidian.helper.ui.widget.RatingBar;
import com.feidian.helper.util.Constant;

public class NewsDetailsActivity extends Activity implements
		View.OnClickListener {


	private ImageView mBackView;
	private TextView mTitle;

	private TextView mNameTv;
	private TextView mStartTimeTv;
	private TextView mTitleTv;
	private TextView mContentTv;
	private TextView mStateTv;
	private TextView mQQTv;
	private TextView mPhoneTv;
	private TextView mPrice;
	private TextView mEndLine;
	private RatingBar ratingBar;
	private TextView mAcceptTv;
	private Button acceptBn;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constant.GET_CONTENT_SUCCESS: {
				Toast.makeText(NewsDetailsActivity.this, "接单成功", Toast.LENGTH_LONG)
				.show();
				getItem().setState("1");
				finish();

			}
				break;
			case Constant.GET_CONTENT_FAILURE: { 

			}
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_details);
		initView();
	}

	private void initView() {
		mBackView = (ImageView) findViewById(R.id.back);
		mBackView.setVisibility(View.VISIBLE);
		mBackView.setOnClickListener(this);
		mTitle = (TextView) findViewById(R.id.title);
		mTitle.setText("懒人帖子详情");
		mNameTv = (TextView) findViewById(R.id.name);

		mStartTimeTv = (TextView) findViewById(R.id.past_time);

		mTitleTv = (TextView) findViewById(R.id.my_title);

		mContentTv = (TextView) findViewById(R.id.content);

		mStateTv = (TextView) findViewById(R.id.state);

		mQQTv = (TextView) findViewById(R.id.qq);

		mPhoneTv = (TextView) findViewById(R.id.phone);

		mPrice = (TextView) findViewById(R.id.price);

		mEndLine = (TextView) findViewById(R.id.endline);

		ratingBar = (RatingBar) findViewById(R.id.ratingBar);

		mAcceptTv = (TextView) findViewById(R.id.accept_name);

		acceptBn = (Button) findViewById(R.id.accept_button);
		acceptBn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(!NetworkDetector.detect(NewsDetailsActivity.this)){
					View view = getLayoutInflater().inflate(R.layout.dialog,null);
		        	final MyDialog dialog = new MyDialog(NewsDetailsActivity.this,view,R.style.dialog);
		        	TextView dialog_title = (TextView)view.findViewById(R.id.dialog_title);
		        	dialog_title.setText("当前网络不可用");
		        	TextView dialog_content = (TextView)view.findViewById(R.id.dialog_content);
		        	dialog_content.setText("是否进行网络设置？");
		        	Button dialog_sure = null;
		        	Button dialog_cancel = null;
		        	dialog_sure = (Button)view.findViewById(R.id.button1);
		        	dialog_cancel = (Button)view.findViewById(R.id.button2);
		        	dialog_sure.setText("是");
                    dialog_cancel.setText("否");
		        	dialog_sure.setOnClickListener(new OnClickListener(){
		        		@Override
		        		public void onClick(View v){
		        			startActivity(new Intent(    
		                            Settings.ACTION_WIRELESS_SETTINGS));
		        		}
		        	});
		        	dialog_cancel.setOnClickListener(new OnClickListener(){
		        		@Override
		        		public void onClick(View view){
		        			dialog.dismiss();
		        		}
		        	});
		        	dialog.show();
				}else{
					if (User.stuId == null || User.stuId.equals("")
							|| User.password == null || User.password.equals("")) {
						Toast.makeText(NewsDetailsActivity.this, "用户尚未登录",
								Toast.LENGTH_LONG).show();
						return;
					}
					
					if(getItem().getName().equals(User.name)){
        				Toast.makeText(NewsDetailsActivity.this, "无法接自己发的单子哦", Toast.LENGTH_SHORT).show();
        			    Log.e("can accept my own danzi","...");
        			    return;
					}
					Log.e("item.mStuId != User.stuId","...");
					View view = getLayoutInflater().inflate(R.layout.dialog,null);
		        	final MyDialog dialog = new MyDialog(NewsDetailsActivity.this,view,R.style.dialog);
		        	TextView dialog_title = (TextView)view.findViewById(R.id.dialog_title);
		        	dialog_title.setText("懒人帮提示");
		        	TextView dialog_content = (TextView)view.findViewById(R.id.dialog_content);
		        	dialog_content.setText("确认接单？");
		        	Button dialog_sure = null;
		        	Button dialog_cancel = null;
		        	dialog_sure = (Button)view.findViewById(R.id.button1);
		        	dialog_cancel = (Button)view.findViewById(R.id.button2);
		        	dialog_sure.setText("确认");
                    dialog_cancel.setText("取消接单");
		        	dialog_sure.setOnClickListener(new OnClickListener(){
		        		@Override
		        		public void onClick(View v){
		        			if (getItem().getState().equals("0")) {
		    					String url = "http://115.29.103.105/User/getHelpObject";
		    					List<NameValuePair> params = new ArrayList<NameValuePair>();
		    					params.add(new BasicNameValuePair(Table.myId,User.stuId));;
		    					params.add(new BasicNameValuePair(Table.id,getItem().getId()));
		    					GetHelpObjectThread getHelpObjectThread = new GetHelpObjectThread(url, params);
		    					getHelpObjectThread.start();
		    				}
		        		}
		        	});
		        	dialog_cancel.setOnClickListener(new OnClickListener(){
		        		@Override
		        		public void onClick(View view){
		        			dialog.dismiss();
		        		}
		        	});
		        	dialog.show();
				}
			}
		});

		upData();

	}

	void upData() {
		if (getItem().getName() != null
				|| !getItem().getName().trim().equals("null"))
			mNameTv.setText(getItem().getName());
		else
			mNameTv.setText("出错");
		mStartTimeTv.setText(getItem().getPastTime());
		mTitleTv.setText(getItem().getTitle());
		mContentTv.setText(getItem().getContent());
		if(getItem().getQQ()!=null&&!getItem().getQQ().equals("null")){
			mQQTv.setText(getItem().getQQ());
		}else{
			mQQTv.setText("无");
		}
		if(getItem().getTel()!=null&&!getItem().getTel().equals("null")){
			mPhoneTv.setText(getItem().getTel());
		}else{
			mPhoneTv.setText("无");
		}
		if(getItem().getPrice()!=null&&!getItem().getPrice().equals("null"))
		{
			mPrice.setText(getItem().getPrice()+"元");
		}else{
			mPrice.setText("0元");
		}
		if(getItem().getEndline()!=null&&!getItem().getEndline().equals("null")){
			mEndLine.setText(getItem().getEndline());
		}else{
			mEndLine.setText("出错");
		}
		if (getItem().getAccepName() != null
				&&!getItem().getAccepName().equals("null"))
			mAcceptTv.setText(getItem().getAccepName());
		else mAcceptTv.setText("无");
		if (getItem().getState().equals("0")) {
			mStateTv.setText("未接单");
		} else if (getItem().getState().equals("1")) {
			mStateTv.setText("处理中");
			acceptBn.setFocusable(false);
			acceptBn.setEnabled(false);
			acceptBn.setText("处理中");
			acceptBn.setBackgroundColor(Color.GRAY);
		} else if (getItem().getState().equals("2")) {
			mStateTv.setText("已经处理");
			acceptBn.setFocusable(false);
			acceptBn.setEnabled(false);
			acceptBn.setText("已经处理");
			acceptBn.setBackgroundColor(Color.GRAY);
		} else {
			mStateTv.setText("出错");
			acceptBn.setFocusable(false);
			acceptBn.setBackgroundColor(Color.GRAY);
		}

		if (getItem().getHonesty() != null
				&& !getItem().getHonesty().equals("null")) {
			ratingBar.setStar(Integer.valueOf(getItem().getHonesty()));
		}

		else {
			ratingBar.setStar(5);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}

	Item getItem() {
		return MessageFragment.itemList.get(MessageFragment.pos);
	}
	
	class GetHelpObjectThread extends Thread
	{
		private String mUrl;
		private List<NameValuePair> mParams;
		public GetHelpObjectThread(String url,List<NameValuePair> params) {
			// TODO Auto-generated constructor stub
			mUrl=url;
			mParams=params;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			String context = NetManager.getNetManager().getWebWithPost(mUrl,
					mParams);
			if (context != null) {

				Log.e("contenxt("+NewsDetailsActivity.this.getClass().toString()+")=", context);
				Message msg = mHandler.obtainMessage();
				msg.obj = context;
				msg.what = Constant.GET_CONTENT_SUCCESS;
				mHandler.sendMessage(msg);
			}

			else
			{
				Log.e("context=", "null");
				Message msg = mHandler.obtainMessage();
				msg.what = Constant.GET_CONTENT_FAILURE;
				mHandler.sendMessage(msg);
			}
				
			super.run();
		}
	}
}
